package com.jdbctest;

import com.example.mybatisdemo.pojo.Studb;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

public class db {

    @Test
    public void DbTest(){
        /* 读取配置文件 将文件读取为流*/
        try {
            InputStream rs = Resources.getResourceAsStream("mybatis-config.xml");
            /* sqlSession SqlSessionFactoryBuilder openSession打开会话 执行sql 语句*/
            SqlSessionFactory sq = new SqlSessionFactoryBuilder().build(rs);

            /*openSession打开会话  执行sql*/
            SqlSession sqlSession = sq.openSession();

            Studb stu = sqlSession.selectOne("findById","3");

            System.out.println(stu.getName());
            System.out.println(stu.getBirthday());
            System.out.println(stu.getStuclass());

            /* 关闭会话*/
            sqlSession.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


}
